package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.bean.GroupSettingBean;
import com.joooonho.SelectableRoundedImageView;

import java.util.List;

/**
 * 群设置 成员adapter
 * Created by Aaron on 16/2/23.
 */
public class GroupSettingNumAdapter extends BaseAdapter {

    private Context ctx;
    private List<GroupSettingBean.TopicEntity.UserListEntity> userList;
    private boolean isAdmin;
    private boolean childGroup;

    public GroupSettingNumAdapter(Context ctx, List<GroupSettingBean.TopicEntity.UserListEntity> userList, boolean isAdmin, boolean childGroup) {
        this.ctx = ctx;
        this.userList = userList;
        this.isAdmin = isAdmin;
        this.childGroup = childGroup;
    }

    public void setAdmin(boolean admin) {
        isAdmin = admin;
    }

    public void setChildGroup(boolean childGroup) {
        this.childGroup = childGroup;
    }

    @Override
    public int getCount() {

        if (childGroup)
            return userList.size();

        if (isAdmin)
            return userList.size() + 2;//如果是群主,则多

        return userList.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_group_num, null);
            viewHolder = new ViewHolder();

            viewHolder.iv_head_pic = (SelectableRoundedImageView) convertView.findViewById(R.id.iv_head_pic);
            viewHolder.tv_nickname = (TextView) convertView.findViewById(R.id.tv_nickname);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (i == userList.size() + 1) {
            //删除群成员的图片
            viewHolder.iv_head_pic.setImageResource(R.mipmap.subtract);
            viewHolder.tv_nickname.setText("");
            return convertView;
        }

        if (i == userList.size()) {
            //添加群成员的图片
            viewHolder.iv_head_pic.setImageResource(R.mipmap.plus);
            viewHolder.tv_nickname.setText("");
            return convertView;
        }

        if (userList == null || userList.get(i) == null || userList.get(i).user == null)
            return convertView;

        //设置昵称
        viewHolder.tv_nickname.setText(userList.get(i).user.nickName);

        //设置头像
        Glide.with(ctx)
                .load(userList.get(i).user.imgUrl)
                .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                .placeholder(R.mipmap.temp_pic)
                .into(viewHolder.iv_head_pic);

        return convertView;
    }

    private class ViewHolder {

        private SelectableRoundedImageView iv_head_pic;
        private TextView tv_nickname;

    }
}